/*dom ui 总控文件
* */
function removeClassWithCheck(ch,cs){
    var dom = $(ch);
    if(dom.hasClass(cs)){
        dom.removeClass(cs);
    }
}

function addClassWithCheck(ch,cs){
    var dom = $(ch);
    if(!dom.hasClass(cs)){
        dom.addClass(cs);
    }
}

function executeWhenEnable(ch,cb){
    var dom = $(ch);
    if(!dom.hasClass("disable") && typeof(cb)==="function"){
        cb();
    }
}

var UISystem = {
    uiLayers : {},
    uiDialogs: {},
    activeLayer:null,
    registerUI:function(name,ui){
        UISystem.uiLayers[name] = ui;
    },
    registerDialog:function(name,dialog){
        UISystem.uiDialogs[name] = dialog;
    },

    hideAll:function(){
        for(var k in UISystem.uiLayers){
            if(UISystem.uiLayers.hasOwnProperty(k)){
                var layer = UISystem.uiLayers[k];
                if(layer.hide && typeof(layer.hide) == "function"){
                    layer.hide();
                }
            }
        }
        UISystem.activeLayer = null;
        for(var k in UISystem.uiDialogs){
            if(UISystem.uiDialogs.hasOwnProperty(k)){
                var dialog = UISystem.uiDialogs[k];
                if(dialog.hide && typeof(dialog.hide) == "function"){
                    dialog.hide();
                }
            }
        }
    },
    /*选择当前显示的UI层，隐藏之前显示的层
    * */
    selectUI:function(name){
        if(UISystem.uiLayers.hasOwnProperty(name)){
            if(UISystem.activeLayer){
                if(UISystem.activeLayer === UISystem.uiLayers[name]){
                    return UISystem.activeLayer;
                }
                if(typeof(UISystem.activeLayer.hide)==="function"){
                    UISystem.activeLayer.hide();
                }else{
                    console.log("UISystem.activeLayer don't has function hide, layer name = %s",UISystem.activeLayer.name);
                }
            }
            UISystem.activeLayer = UISystem.uiLayers[name];
            if(typeof(UISystem.activeLayer.show) ==="function"){
                UISystem.activeLayer.show();
                return UISystem.activeLayer;
            }else{
                console.log("UISystem.activeLayer don't has function show, layer name = %s",UISystem.activeLayer.name);
            }
        }else{
            console.warn("select ui not exists, name = %s",name);
        }
    },
    /*执行当前活跃UI层的函数
    */
    activeLayerApply:function(){
        var funname = arguments[0];
        if(UISystem.activeLayer){
            if(UISystem.activeLayer.hasOwnProperty(funname) && typeof(UISystem.activeLayer[funname])=="function"){
                var fun = UISystem.activeLayer[funname];
                return fun.apply(fun,Array.prototype.slice.call(arguments,1));
            }else{
                console.warn("activeLayer dont have function, name = %s",funname);
            }
        }
    },
    layerApply:function(){
        var layerName = arguments[0];
        var funname = arguments[1];
        if(UISystem.uiLayers.hasOwnProperty(layerName)){
            var layer = UISystem.uiLayers[layerName];
            if(layer.hasOwnProperty(funname)){
                var fun = layer[funname];
                return fun.apply(fun,Array.prototype.slice.call(arguments,2));
            }else{
                console.warn("UI %s not have function %s",layerName,funname);
            }
        }else{
            console.warn("UI %s not find",layerName);
        }
    },
    getDialog:function(name){
        if(UISystem.uiDialogs.hasOwnProperty(name)){
            var dialog = UISystem.uiDialogs[name];
            return dialog;
        }else{
            console.warn("Dialog %s not find",name);
        }
    },
    showDialog:function(name){
        if(UISystem.uiDialogs.hasOwnProperty(name)){
            var dialog = UISystem.uiDialogs[name];
            if(dialog.show && typeof dialog.show === "function"){
                dialog.show();
            }else{
                console.warn("Dialog %s not have function show",name);
            }
        }else{
            console.warn("Dialog %s not find",name);
        }
    }
}